#include "non_threshold_evaluator.h"

#include <fstream>
#include <iostream>

double average_precision_evaluator::evaluate (vector<int> & label_list, vector<double> & score_list, string output, int bin) {
	int l = (int) label_list.size();

	int tp = 0, tn = 0, fp = 0, fn = 0;
	for(int i = 0; i < l; i++) {
		if(label_list[i] == 1) {
			fn ++;
		} else {
			tn ++;
		}
	}
	int pl = fn;

	threshold_evaluator * x = new precision();

	double sum = 0;
	for(int i = 0; i < l; i++) {
		int rel_k = label_list[i] == 1 ? 1 : 0;
		if(rel_k == 1) {
			tp ++;
			fn --;
		} else {
			fp ++;
			tn --;
		}

		if(rel_k == 1) {
			double pk = x->get_value(tp, tn, fp, fn);
			sum += pk;
		}
	}

	double avep = sum / pl;
	ofstream fout(output.c_str());
	fout << "average precision = " << avep << endl;
	cout << "average precision = " << avep << endl;
	fout.close();

	delete x;
	return avep;
}
